window.addEventListener('DOMContentLoaded', () => {
    const btnElement = document.getElementById('btn');
    const setTitleElement = document.getElementById('setTitle');
    const openFileElement = document.getElementById('openFile')
    const filePathElement = document.getElementById('filePath');
    const counter = document.getElementById('counter');
    const closeElement = document.getElementById('close');
    const maximizeElement = document.getElementById('maximize');
    const minimizeElement = document.getElementById('minimize');
    const menuInputElement = document.getElementById('menuInput');
    const addMenuButtonElement = document.getElementById('addMenuButton');
    const fromNewElement=document.getElementById('fromNew');
    const openErrElement=document.getElementById('openErr');
    const openURLElement=document.getElementById('openURL');
    const createNotificationElement=document.getElementById('createNotification');
    const copyValueElement=document.getElementById('copyValue');
    const copyBtnElement=document.getElementById('copyBtn');
    const pasteValueElement=document.getElementById('pasteValue');
    const pasteBtnElement=document.getElementById('pasteBtn');
    const imgValueElement=document.getElementById('imgValue');
    const imgElement=document.getElementById('img');
    //打开新窗口
    btnElement.addEventListener('click', async () => {
        const value = await window.electronAPI.newBrowserWindow();
        console.log('value', value)
        localStorage.setItem('name','mickey')
    })
    //设置新标题
    setTitleElement.addEventListener('click', () => {
        window.electronAPI.setTitle('新标题');
    })
    //选择并获取文件路径
    openFileElement.addEventListener('click', async () => {
        const filePath = await window.electronAPI.openFile()
        filePathElement.innerText = filePath
    })
    //处理更新计数器
    window.electronAPI.updateCounter((value) => {
        const oldValue = Number(counter.innerText)
        const newValue = oldValue + value
        counter.innerText = newValue.toString()
    })
    //关闭窗口
    closeElement.addEventListener('click', () => {
        window.electronAPI.close();
    })
    //窗口最大化
    maximizeElement.addEventListener('click', () => {
        window.electronAPI.maximize();
    })
    //窗口最小化
    minimizeElement.addEventListener('click', () => {
        window.electronAPI.minimize();
    })
    //阻止窗口关闭
    // window.onbeforeunload=(e)=>{
    //     console.log('阻止关闭');
    //     e.returnValue=false;
    // }
    //设置菜单
    addMenuButtonElement.addEventListener('click', () => {
        console.log('menuInputElement', menuInputElement.value)
        window.electronAPI.setMenu(menuInputElement.value);
    })
    //右键菜单
    window.addEventListener('contextmenu', (e) => {
        e.preventDefault();
        window.electronAPI.showContextMenu();
    })
    //右键菜单点击事件
    window.electronAPI.contextMenuCommand((value) => {
        console.log('value', value)
    })
    //
    window.electronAPI.toMsg((value)=>{
        console.log('value',value)
        fromNewElement.innerText=value
    })
    //打开错误对话框
    openErrElement.addEventListener('click',()=>{
        window.electronAPI.openErr();
    })
    //打开URL
    openURLElement.addEventListener('click',()=>{
        window.electronAPI.openURL()
    })
    //打开消息通知
    createNotificationElement.addEventListener('click',()=>{
        window.electronAPI.createNotification();
    })
    //复制剪切板
    copyBtnElement.addEventListener('click',()=>{
        const value=copyValueElement.value;
        window.electronAPI.copyValue(value);
    })
    //粘贴剪切板
    pasteBtnElement.addEventListener('click',async()=>{
        pasteValueElement.value=await window.electronAPI.pasteValue();
    })
    //复制粘贴图片到剪切板
    imgValueElement.addEventListener('click',async()=>{
        const res=await window.electronAPI.imgValue();
        imgElement.src=res.toDataURL();
    })
})